<template>
  <div>
    <h2>登录页面</h2>
    <table class="table" border="1">
      <tr>
      <td>用户名</td>
      <td>
        <input type="text" v-model="state.Uname">
      </td>
    </tr>
    <tr>
      <td>密码</td>
      <td>
        <input type="password" v-model="state.Upwd">
      </td>
    </tr>
    <tr>
      <td colspan="2"><input type="button" value="登录" @click="login"></td>
    </tr>
    </table>
    
  </div>
</template>

<script setup lang="ts">
import router from '@/router';
import {useStore} from '../store/user'
const userstore=useStore();
import axios from 'axios';
import {reactive,toRefs} from 'vue'
let data=reactive({
  state:{
    Uname:"",
    Upwd:""
  },
  list:{}
})
let {state,list}=toRefs(data)


function login()
{
  if(state.value.Upwd=="")
{
  alert("密码不能为空");
  return;
}
  axios({
    url:"http://localhost:51322/api/User/GetUserLogin",
    method:"get",
    params:state.value
  })
  .then(res=>{
    console.log(res);
    if(res.data.data==1)
  {
    axios({
      url:"http://localhost:51322/api/User/GetUserType",
      method:"get",
      params:{
        Uname:state.value.Uname
      }
    })
    .then(res=>{
      console.log(res);
      list.value=res.data.data[0]
      userstore.userInfo=list.value
      if(userstore.userInfo.UT_name=="管理员")
    {
        alert("管理员登录成功");
        router.push("/UserLoginList")
    }
    else
    {
        alert("用户登录成功");
        router.push("/UserLoginList")
    }
    })
    .catch(err=>{
      console.log(err);
    })
  }
  else if(res.data.data==-1)
  {
    alert("密码错误");
    return;
  }
  else if(res.data.data==-2)
  {
    alert("用户名不存在");;
    return;
  }
  })
  .catch(err=>{
    console.log(err);
  })

}
</script>

<style scoped>

</style>